package com.bang.study.jvm.metespace;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName: DynamicProxyTest
 * @Desc: java动态代理测试
 * @auther: BANG
 * @Date: 2019/09/29 08:52
 * @Version: 1.0
 * @Modified By:
 * @VM -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m -Xmx20m -Xmx20m
 */
public class DynamicProxyTest {

    /**
     * @Desc:
     * @Author: <BANG>
     * @Date: 2019/9/29 0029 上午 9:00
     * @Param: args
     * @Return: void
     * @VM -XX:+TraceClassLoading -XX:+TraceClassUnloading
     */
    public static void main(String[] args) throws Exception {
       final IParentService parentService = new SonServiceImpl();
       while (true){
           Object proxyInstance = Proxy.newProxyInstance(parentService.getClass().getClassLoader(), parentService.getClass().getInterfaces(), new InvocationHandler() {
               @Override
               public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                   System.out.println("do something pre");
                   Object invoke = method.invoke(parentService, args);
                   System.out.println("do something after");
                   return invoke;
               }
           });
           Class<?> clazz = proxyInstance.getClass();
           String name = clazz.getName();
           Class<?> superclass = clazz.getSuperclass();
           Class<?>[] interfaces = clazz.getInterfaces();
           ((IParentService) proxyInstance).run("小明");
       }


    }

}
